package com.roadmap.struts.action.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.roadmap.biz.common.IManager;
import com.roadmap.biz.common.ManagerFactory;
import com.roadmap.common.RdcConst;
import com.roadmap.entities.Aspirant;
import com.roadmap.entities.Career;
import com.roadmap.entities.Contact;
import com.roadmap.entities.Student;
import com.roadmap.entities.ext.AspirantExt;
import com.roadmap.entities.ext.StudentExt;
import com.roadmap.struts.action.ActionConst;
import com.roadmap.struts.action.RdcAction;
import com.roadmap.struts.form.admin.CareerExt;

public class ProfileAction extends RdcAction {
	public ProfileAction() {
		super("getprofile");
	}

	@Override
	public ActionForward performAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String ownerId = request
				.getParameter(ActionConst.Aspirant_Id_In_Request);
		if (ownerId == null || ownerId.trim().length() == 0) {
			return handleInvalidUser(request, "/infor.do");
		}

		String currentUser = getUIDFromSession(request);
		onPrepare1(request, currentUser, ownerId);
		onPrepare2(request, currentUser, ownerId);
		onPrepare3(request, currentUser, ownerId);
		onPrepare4(request, currentUser, ownerId);

		return mapping.findForward("getprofile");
	}

	private void onPrepare1(HttpServletRequest request, String currentUser,
			String itemOwner) throws Exception {
		IManager mgr = ManagerFactory.getInstance(RdcConst.CONTACT,
				currentUser, itemOwner);
		Contact contact = null;
		List<Contact> list =  mgr.getEntitiesOfOwner();
		if (list != null && !list.isEmpty()) {
			contact = list.get(0);

		}
		else {
			// 没有相应的记录则创建一个没有数据的新实体
			contact = new Contact();
		}

		request.setAttribute("contactForm", contact);
	}

	private void onPrepare2(HttpServletRequest request, String currentUser,
			String itemOwner) throws Exception {
		IManager mgr = ManagerFactory.getInstance(RdcConst.CAREER, currentUser,
				itemOwner);
		Career career = null;
		List<Career> list = mgr.getEntitiesOfOwner();
		if (list != null && !list.isEmpty()) {
			career = list.get(0);
		}

		request.setAttribute("careerForm", new CareerExt(career));
	}

	private void onPrepare3(HttpServletRequest request, String currentUser,
			String itemOwner) throws Exception {
		IManager mgr = ManagerFactory.getInstance(RdcConst.STUDENT,
				currentUser, itemOwner);
		Student stu = null;
		List<Student> list =  mgr.getEntitiesOfOwner();
		if (list != null && !list.isEmpty()) {
			stu = list.get(0);
		}
		
		request.setAttribute("studentForm", new StudentExt(stu));
	}

	private void onPrepare4(HttpServletRequest request, String currentUser,
			String itemOwner) throws Exception {
		IManager mgr = ManagerFactory.getInstance(RdcConst.USER, currentUser,
				itemOwner);
		Aspirant asp = null;
		List<Aspirant> list =  mgr.getEntitiesOfOwner();
		if (list != null && !list.isEmpty()) {
			asp = list.get(0);
		} 
		
		request.setAttribute("basicInfoForm", new AspirantExt(asp));
	}

	@Override
	public boolean isFormSubmitted(HttpServletRequest request) throws Exception {
		return false;
	}

}